/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Gui.java
 *
 * Created on 6 mai 2010, 08:34:17
 */

package robotdanceur;

import bluenext.ErreurNXTException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;


/**
 *
 * @author 
 */


public class Gui extends javax.swing.JFrame {

    /** Creates new form Gui */
    private Ctrl ctrl;
    private String nomsRobots[];
    public Gui(Ctrl controleur) {
        ctrl = controleur;
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jBsearch = new javax.swing.JButton();
        list1 = new java.awt.List();
        jBconnexion = new javax.swing.JButton();
        jBdeconnexion = new javax.swing.JButton();
        jBenvoyerFichier = new javax.swing.JButton();
        jBstart = new javax.swing.JButton();
        textFieldFichier = new java.awt.TextField();
        textFieldNomPrg = new java.awt.TextField();
        label2 = new java.awt.Label();
        textFieldStart = new java.awt.TextField();
        jBfichierSource = new javax.swing.JButton();
        jBenvoyerDonnee = new javax.swing.JButton();
        textFieldData = new java.awt.TextField();
        jCheckBox1 = new javax.swing.JCheckBox();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBsearch.setActionCommand("search");
        jBsearch.setLabel("search");
        jBsearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSearchActionPerformed(evt);
            }
        });

        list1.setMultipleMode(true);

        jBconnexion.setText("connexion");
        jBconnexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConnexionActionPerformed(evt);
            }
        });

        jBdeconnexion.setText("deconnexion");
        jBdeconnexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeconnecxionActionPerformed(evt);
            }
        });

        jBenvoyerFichier.setText("envoyer fichier");
        jBenvoyerFichier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSendFichierActionPerformed(evt);
            }
        });

        jBstart.setText("start");
        jBstart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStartPrgActionPerformed(evt);
            }
        });

        textFieldFichier.setName("cheminFichier"); // NOI18N

        textFieldNomPrg.setName("nomPrg"); // NOI18N

        label2.setText("nom programme");

        jBfichierSource.setText("fichier source");
        jBfichierSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FichierSourceActionPerformed(evt);
            }
        });

        jBenvoyerDonnee.setLabel("envoyer données");
        jBenvoyerDonnee.setName("envoyer données"); // NOI18N
        jBenvoyerDonnee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBsendDataActionPerformed(evt);
            }
        });

        textFieldData.setName("donnees"); // NOI18N

        jCheckBox1.setText("reconnexion");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jBenvoyerDonnee, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBstart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBdeconnexion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBconnexion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jBfichierSource, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(label2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(textFieldNomPrg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(textFieldFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jBenvoyerFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(159, 159, 159))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(textFieldData, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(textFieldStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCheckBox1)
                                .addGap(225, 225, 225))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBsearch, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                        .addGap(429, 429, 429))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(jBsearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBconnexion)
                                .addGap(11, 11, 11)
                                .addComponent(jBdeconnexion)
                                .addGap(5, 5, 5)
                                .addComponent(textFieldFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jBfichierSource))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textFieldNomPrg, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBenvoyerFichier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jBenvoyerDonnee)
                                    .addComponent(textFieldData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jBstart, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textFieldStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jCheckBox1)))
                    .addComponent(list1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        list1.getAccessibleContext().setAccessibleName("listRobot");
        textFieldFichier.getAccessibleContext().setAccessibleName("prgName");
        textFieldNomPrg.getAccessibleContext().setAccessibleName("sendPrgName");
        textFieldStart.getAccessibleContext().setAccessibleName("nomPrgNXT");
        jBenvoyerDonnee.getAccessibleContext().setAccessibleName(null);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSearchActionPerformed
        try {
            // TODO add your handling code here:
            nomsRobots = ctrl.Search();
        } catch (ErreurNXTException ex) {
            Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
        }
        list1.clear();
        if (nomsRobots == (null) || nomsRobots.length == 0) {// | nomsRobots.length==0){
            System.out.println("Aucun NXT a proximite");
            list1.add("Aucun NXT a proximite");
        } else {
            for (int i = 0; i < nomsRobots.length; i++) {
                System.out.println(i + ". " + nomsRobots[i]);
                list1.add(nomsRobots[i]);
            }
        }

    }//GEN-LAST:event_jbSearchActionPerformed

    private void jbConnexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConnexionActionPerformed
        // TODO add your handling code here:
        String robot[] = list1.getSelectedItems();
        boolean resultat;

        for(int i =0; i < robot.length; i++)
        {
            try{
            resultat = ctrl.Connecter(robot[i]);
            System.out.println("Connexion a " + robot[i] + " : " + resultat);
            } catch(ErreurNXTException ex){
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
            //catch(){}
        }
    }//GEN-LAST:event_jbConnexionActionPerformed

    private void jbDeconnecxionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDeconnecxionActionPerformed
        // TODO add your handling code here:
         String robot[] = list1.getSelectedItems();
         for (int i =0; i< robot.length; i++)
         {
            try {
                ctrl.Deconnecter(robot[i]);
            } catch (IOException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
    }//GEN-LAST:event_jbDeconnecxionActionPerformed

    private void jbSendFichierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSendFichierActionPerformed
        // TODO add your handling code here:
        String robot[] = list1.getSelectedItems();
        textFieldStart.setText(textFieldNomPrg.getText());
        for (int i = 0; i < robot.length; i++) {
            try {
                try {
                    ctrl.SendProg(robot[i], textFieldFichier.getText(), textFieldNomPrg.getText());
                } catch (IOException ex) {
                    Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (ErreurNXTException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jbSendFichierActionPerformed

    private void jbStartPrgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStartPrgActionPerformed
        // TODO add your handling code here:
         String robot[] = list1.getSelectedItems();
         for (int i =0; i< robot.length; i++)
         {
            try {
                ctrl.StartProg(robot[i], textFieldStart.getText());
            } catch (IOException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ErreurNXTException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (jCheckBox1.isSelected())
            {
                try {
                    ctrl.Deconnecter(robot[i]);
                } catch (IOException ex) {
                    Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    ctrl.Connecter(robot[i]);
                } catch (ErreurNXTException ex) {
                    Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
         }
    }//GEN-LAST:event_jbStartPrgActionPerformed

    private void FichierSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FichierSourceActionPerformed
        // TODO add your handling code here:
       JFileChooser ChoixFichier = new JFileChooser() ;
       if (ChoixFichier.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
       {
           textFieldFichier.setText(ChoixFichier.getSelectedFile().getAbsolutePath()); //si un fichier est selectionné, récupérer le fichier puis sont path et l'afficher dans le champs de texte
           textFieldNomPrg.setText(ChoixFichier.getSelectedFile().getName());
       }
    }//GEN-LAST:event_FichierSourceActionPerformed

    private void jBsendDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBsendDataActionPerformed
        // TODO add your handling code here:
        String robot[] = list1.getSelectedItems();
        String mess = textFieldData.getText();
        System.out.println("text : "+mess+"\n");
        byte[] data = mess.getBytes();
        for (int i =0; i< robot.length; i++)
        {
            try {
                ctrl.SendData(robot[i], data);
            } catch (IOException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ErreurNXTException ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_jBsendDataActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBconnexion;
    private javax.swing.JButton jBdeconnexion;
    private javax.swing.JButton jBenvoyerDonnee;
    private javax.swing.JButton jBenvoyerFichier;
    private javax.swing.JButton jBfichierSource;
    private javax.swing.JButton jBsearch;
    private javax.swing.JButton jBstart;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JDialog jDialog1;
    private java.awt.Label label2;
    private java.awt.List list1;
    private java.awt.TextField textFieldData;
    private java.awt.TextField textFieldFichier;
    private java.awt.TextField textFieldNomPrg;
    private java.awt.TextField textFieldStart;
    // End of variables declaration//GEN-END:variables

}
